<template>
  <div class="login_page fillcontain">
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer" v-show="showLogin">
        <div class="manage_tip">
          <p>学习任务后台管理系统</p>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginForm" v-if="login">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名"
              ><span>dsfsf</span></el-input
            >
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="密码"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('loginForm')"
              class="submit_btn"
              >登陆</el-button
            >
            <el-button type="primary" @click="regname()" class="submit_btn"
              >注册</el-button
            >
          </el-form-item>
        </el-form>
        <div v-if="!login">
          <el-form :model="regForm" :rules="regRules" ref="regForm">
            <el-form-item prop="username">
              <el-input v-model="regForm.username" placeholder="用户名"
                ><span>dsfsf</span></el-input
              >
            </el-form-item>
            <el-form-item prop="nickname">
              <el-input
                type="text"
                placeholder="昵称"
                v-model="regForm.nickname"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                placeholder="密码"
                v-model="regForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item prop="renpassword">
              <el-input
                type="password"
                placeholder="确认密码"
                v-model="regForm.renpassword"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                @click="submitForm('regForm')"
                class="submit_btn"
                >注册</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <!-- <p class="tip">温馨提示：</p>
				<p class="tip">未登录过的新用户，自动注册</p>
				<p class="tip">注册过的用户可凭账号密码登录</p> -->
      </section>
    </transition>
  </div>
</template>

<script>
import { getTeacherLogin, getAdminInfo, TeacherRegister } from "@/api/getData";
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      regForm: {
        username: "",
        password: "",
        nickname: "",
        renpassword: "",
      },
      regRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        renpassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
        ],
      },
      showLogin: false,
      login: true,
    };
  },
  mounted() {
    this.showLogin = true;
    if (!this.adminInfo.id) {
      this.getAdminData();
    }
  },
  computed: {
    ...mapState(["adminInfo"]),
  },
  methods: {
    ...mapActions(["getAdminData"]),
    // this.$router.push('manage');
    async submitForm(formName) {
      let that = this;
      if (formName == "loginForm") {
        this.teacherLogin(formName);
      } else if (formName == "regForm") {
    
        let param = new URLSearchParams();
        param.append("username", this.regForm.username);
        param.append("password", this.regForm.password);
        param.append("repassword", this.regForm.renpassword);
        param.append("nickname", this.regForm.nickname);
     
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let res = await TeacherRegister(param);
            console.log(res);
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: res.data.msg,
              });

              that.teacherLogin("loginForm", "login"); //注册后登录
              this.$router.push("shopList");
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
              });
            }
          } else {
            this.$notify.error({
              title: "错误",
              message: "请输入正确的用户名密码",
              offset: 100,
            });
            return false;
          }
        });
      }
    },
    async teacherLogin(formName, login) {
      if (!login) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let params = new URLSearchParams();
            console.log(this.loginForm.username);

            params.append("username", this.loginForm.username);
            params.append("password", this.loginForm.password);

            let res = await getTeacherLogin(params);
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: res.data.msg,
              });
              this.$router.push("shopList");

              localStorage.setItem("userInfo", JSON.stringify(res.data.data));
            } else {
              this.$message({
                type: "error",
                message: res.data.msg,
              });
            }
          } else {
            this.$notify.error({
              title: "错误",
              message: "请输入正确的用户名密码",
              offset: 100,
            });
            return false;
          }
        });
      } else {
        let params = new URLSearchParams();
        params.append("username", this.regForm.username);
        params.append("password", this.regForm.password);
        console.log(params);
        let res = await getTeacherLogin(params);
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: res.data.msg,
          });
          this.$router.push("shopList");
          localStorage.setItem("nickname", res.data.data.nickname);
          localStorage.setItem("userId", res.data.data.id);
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      }
    },

    regname() {
      this.login = !this.login;
    },
  },
  watch: {
    adminInfo: function (newValue) {
      if (newValue.id) {
        this.$message({
          type: "success",
          message: "检测到您之前登录过，将自动登录",
        });
        this.$router.push("shopList");
      }
    },
  },
};
</script>

<style lang="less" scoped>
@import "../style/mixin";
.login_page {
  background-color: #324057;
}
.manage_tip {
  position: absolute;
  width: 100%;
  top: -100px;
  left: 0;
  p {
    font-size: 34px;
    color: #fff;
  }
}
.form_contianer {
  .wh(320px, 260px);
  .ctp(320px, 210px);
  padding: 25px;
  border-radius: 5px;
  text-align: center;
  background-color: #fff;
  .submit_btn {
    width: 100%;
    font-size: 16px;
    margin-left: 0 !important;
    margin-bottom: 20px;
  }
}
.tip {
  font-size: 12px;
  color: red;
}
.form-fade-enter-active,
.form-fade-leave-active {
  transition: all 1s;
}
.form-fade-enter,
.form-fade-leave-active {
  transform: translate3d(0, -50px, 0);
  opacity: 0;
}
</style>
